package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.PigDAO;
import cn.edu.scau.cmi.hombio.dao.SaleDAO;
import cn.edu.scau.cmi.hombio.dao.SalePigDAO;

import cn.edu.scau.cmi.hombio.domain.Pig;
import cn.edu.scau.cmi.hombio.domain.Sale;
import cn.edu.scau.cmi.hombio.domain.SalePig;

import cn.edu.scau.cmi.hombio.service.SalePigService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for SalePig entities
 * 
 */

@Controller("SalePigRestController")

public class SalePigRestController {

	/**
	 * DAO injected by Spring that manages Pig entities
	 * 
	 */
	@Autowired
	private PigDAO pigDAO;

	/**
	 * DAO injected by Spring that manages Sale entities
	 * 
	 */
	@Autowired
	private SaleDAO saleDAO;

	/**
	 * DAO injected by Spring that manages SalePig entities
	 * 
	 */
	@Autowired
	private SalePigDAO salePigDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for SalePig entities
	 * 
	 */
	@Autowired
	private SalePigService salePigService;

	/**
	 * Delete an existing Pig entity
	 * 
	 */
	@RequestMapping(value = "/SalePig/{salepig_id}/pig/{pig_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteSalePigPig(@PathVariable Integer salepig_id, @PathVariable Integer related_pig_id) {
		salePigService.deleteSalePigPig(salepig_id, related_pig_id);
	}

	/**
	* Save an existing Sale entity
	* 
	*/
	@RequestMapping(value = "/SalePig/{salepig_id}/sale", method = RequestMethod.PUT)
	@ResponseBody
	public Sale saveSalePigSale(@PathVariable Integer salepig_id, @RequestBody Sale sale) {
		salePigService.saveSalePigSale(salepig_id, sale);
		return saleDAO.findSaleByPrimaryKey(sale.getId());
	}

	/**
	* Delete an existing SalePig entity
	* 
	*/
	@RequestMapping(value = "/SalePig/{salepig_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteSalePig(@PathVariable Integer salepig_id) {
		SalePig salepig = salePigDAO.findSalePigByPrimaryKey(salepig_id);
		salePigService.deleteSalePig(salepig);
	}

	/**
	* Select an existing SalePig entity
	* 
	*/
	@RequestMapping(value = "/SalePig/{salepig_id}", method = RequestMethod.GET)
	@ResponseBody
	public SalePig loadSalePig(@PathVariable Integer salepig_id) {
		return salePigDAO.findSalePigByPrimaryKey(salepig_id);
	}

	/**
	* Save an existing SalePig entity
	* 
	*/
	@RequestMapping(value = "/SalePig", method = RequestMethod.PUT)
	@ResponseBody
	public SalePig saveSalePig(@RequestBody SalePig salepig) {
		salePigService.saveSalePig(salepig);
		return salePigDAO.findSalePigByPrimaryKey(salepig.getId());
	}

	/**
	* Create a new Sale entity
	* 
	*/
	@RequestMapping(value = "/SalePig/{salepig_id}/sale", method = RequestMethod.POST)
	@ResponseBody
	public Sale newSalePigSale(@PathVariable Integer salepig_id, @RequestBody Sale sale) {
		salePigService.saveSalePigSale(salepig_id, sale);
		return saleDAO.findSaleByPrimaryKey(sale.getId());
	}

	/**
	* Save an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/SalePig/{salepig_id}/pig", method = RequestMethod.PUT)
	@ResponseBody
	public Pig saveSalePigPig(@PathVariable Integer salepig_id, @RequestBody Pig pig) {
		salePigService.saveSalePigPig(salepig_id, pig);
		return pigDAO.findPigByPrimaryKey(pig.getId());
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Show all SalePig entities
	* 
	*/
	@RequestMapping(value = "/SalePig", method = RequestMethod.GET)
	@ResponseBody
	public List<SalePig> listSalePigs() {
		return new java.util.ArrayList<SalePig>(salePigService.loadSalePigs());
	}

	/**
	* View an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/SalePig/{salepig_id}/pig/{pig_id}", method = RequestMethod.GET)
	@ResponseBody
	public Pig loadSalePigPig(@PathVariable Integer salepig_id, @PathVariable Integer related_pig_id) {
		Pig pig = pigDAO.findPigByPrimaryKey(related_pig_id, -1, -1);

		return pig;
	}

	/**
	* View an existing Sale entity
	* 
	*/
	@RequestMapping(value = "/SalePig/{salepig_id}/sale/{sale_id}", method = RequestMethod.GET)
	@ResponseBody
	public Sale loadSalePigSale(@PathVariable Integer salepig_id, @PathVariable Integer related_sale_id) {
		Sale sale = saleDAO.findSaleByPrimaryKey(related_sale_id, -1, -1);

		return sale;
	}

	/**
	* Delete an existing Sale entity
	* 
	*/
	@RequestMapping(value = "/SalePig/{salepig_id}/sale/{sale_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteSalePigSale(@PathVariable Integer salepig_id, @PathVariable Integer related_sale_id) {
		salePigService.deleteSalePigSale(salepig_id, related_sale_id);
	}

	/**
	* Create a new SalePig entity
	* 
	*/
	@RequestMapping(value = "/SalePig", method = RequestMethod.POST)
	@ResponseBody
	public SalePig newSalePig(@RequestBody SalePig salepig) {
		salePigService.saveSalePig(salepig);
		return salePigDAO.findSalePigByPrimaryKey(salepig.getId());
	}

	/**
	* Get Sale entity by SalePig
	* 
	*/
	@RequestMapping(value = "/SalePig/{salepig_id}/sale", method = RequestMethod.GET)
	@ResponseBody
	public Sale getSalePigSale(@PathVariable Integer salepig_id) {
		return salePigDAO.findSalePigByPrimaryKey(salepig_id).getSale();
	}

	/**
	* Get Pig entity by SalePig
	* 
	*/
	@RequestMapping(value = "/SalePig/{salepig_id}/pig", method = RequestMethod.GET)
	@ResponseBody
	public Pig getSalePigPig(@PathVariable Integer salepig_id) {
		return salePigDAO.findSalePigByPrimaryKey(salepig_id).getPig();
	}

	/**
	* Create a new Pig entity
	* 
	*/
	@RequestMapping(value = "/SalePig/{salepig_id}/pig", method = RequestMethod.POST)
	@ResponseBody
	public Pig newSalePigPig(@PathVariable Integer salepig_id, @RequestBody Pig pig) {
		salePigService.saveSalePigPig(salepig_id, pig);
		return pigDAO.findPigByPrimaryKey(pig.getId());
	}
}